﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FluentData;
using HtmlAgilityPack;
using TaoBaoKe.Common;
using System.Net;
using System.Text.RegularExpressions;
namespace FormTBK.Function
{
    public partial class GetKeyFromTB : Form
    {
        IDbContext dbContext = new DbContext().ConnectionString("server=.;User ID=sa;Password=111111;database=test;Connection Reset=FALSE", new SqlServerProvider());
        public GetKeyFromTB()
        {
            InitializeComponent();
        }

        private void GetKeyFromTB_Load(object sender, EventArgs e)
        {

        }

        private void btnGetKey_Click(object sender, EventArgs e)
        {
            List<string> urls = new List<string>();
            HtmlWeb w = new HtmlWeb();
            w.OverrideEncoding = Encoding.GetEncoding("gb2312");
            w.UseCookies = false;
            w.NeedParse = false;
            w.PreRequest += req;
            HtmlAgilityPack.HtmlDocument d = w.Load("http://top.taobao.com/index.php");
            string result = d.Text.Replace("\n","").Replace("\r","");
            Match m= result.Match("<div\\sid=\"nav\">.*?</div>");
            string tempstr = "";
            if (m.Value!="")
            {
                MatchCollection mcc= m.Value.Matches("data-href=\"(?<url>.*?)\">.*?<span>(?<name>.*?)</span>");
                MatchCollection tempmcc = tempstr.Matches("<a\\s.*?href=\"(?<url>.*?)\".*?/a>");
                if (mcc.Count > 0)
                {
                    for (int i = 0; i < mcc.Count; i++)
                    {
                        d= w.Load(mcc[i].Groups["url"].Value);
                         tempstr = d.Text;
                        tempstr = tempstr.Replace("\\n", "").Replace("\r","").Replace("\\\"","\"").Replace("\\/","/");
                        tempmcc =tempstr.Matches("<a\\s.*?href=\"(?<url>.*?)\".*?/a>");
                        for (int ti = 0; ti < tempmcc.Count; ti++)
                        {
                            urls.Add(tempmcc[ti].Groups["url"].Value);
                        }
                    }
                }
                else
                {
                    this.textBox1.Text = "未发现数据";
                }
                if (urls.Count>0)
                {
                    for (int urlsi = 0; urlsi < urls.Count; urlsi++)
                    {
                        if (urlsi<253)
                        {
                            continue;
                        }
                        string urlBase = "http://top.taobao.com";
                        int offset = 0;
                        if (urlsi==253)
                        {
                            offset = 390;
                        }
                        string up = "false";
                        string param = "&show=focus&up=" + up+ "&offset=";
                        //this.textBox1.AppendText(urlBase + urls[urlsi] + "" + param + offset + Environment.NewLine);
                        while (true)
                        {
                            string url = urlBase + urls[urlsi] + "" + param + offset;
                            d = w.Load(url);
                            result = d.Text;
                            result = result.Replace("\n", "").Replace("\r", "");
                            m = result.Match("<table\\sclass=\"textlist\">.*?</table>");
                            if (m.Value != "")
                            {
                                mcc = m.Value.Matches("<a.*?>(?<name>.*?)</a>");
                                if (mcc.Count == 0)
                                {
                                    break;
                                }
                                this.textBox1.AppendText("采集到" + mcc.Count + Environment.NewLine);
                                for (int mcci = 0; mcci < mcc.Count; mcci++)
                                {
                                    tbk_KeywordByTaoBao md = new tbk_KeywordByTaoBao();
                                    md.Keyword = mcc[mcci].Groups["name"].Value;
                                    md.SeedId = urlsi;
                                    dbContext.Insert<tbk_KeywordByTaoBao>("tbk_KeywordByTaoBao", md).AutoMap(mx => mx.Kid).Execute();
                                }
                                this.textBox1.AppendText("等五秒" + Environment.NewLine);
                                System.Threading.Thread.Sleep(1000);
                            }
                            else
                            {
                                break;
                            }
                            offset += 30;
                        }
                    }
                    dbContext.Dispose();
                    MessageBox.Show("完成");
                }
            }     
        }
        private bool req(System.Net.HttpWebRequest request)
        {
            request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8");
            request.Headers.Add("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
            request.Headers.Add("X-Requested-With", "XMLHttpRequest");
            request.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
            request.AutomaticDecompression = DecompressionMethods.GZip;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";
            return true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
    public class tbk_KeywordByTaoBao
    {
        public int Kid { get; set; }
        public string Keyword { get; set; }
        public int SeedId { get; set; }
    }
}
